package com.sms.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.sms.client.ui.CMenuItem;
import com.sms.client.ui.DashBoardUI;
import com.sms.client.ui.LiveProductionUI;
import com.sms.client.ui.LoginUI;
import com.sms.client.ui.MeterListView;
import com.sms.client.ui.ReportUI;
import com.sms.shared.SessionInfo;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SMS implements EntryPoint {
	
	/**
	 * This is the entry point method.
	 */
	
	private FlexTable appMenuTables;
	public void onModuleLoad() {
		
		final FlexTable headerFlexTable = new FlexTable();
		headerFlexTable.setWidth("100%");
		final FlexTable loginFlexTable = new FlexTable();
		loginFlexTable.setWidth("100%");
//		loginFlexTable.setWidget(0, 0, new HTML("<h1>Solar Monitoring System </h1>"));
//		headerFlexTable.setWidget(1,0, new HTML(""));
		
		
		
		final Anchor loginLink = new Anchor("Login");
		
		
		loginFlexTable.setWidget(0, 1, loginLink);
		loginLink.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				new LoginUI(loginLink.getAbsoluteLeft()-340,loginLink.getAbsoluteTop(),new AsyncCallback<SessionInfo>() {

					@Override
					public void onFailure(Throwable caught) {
						caught.printStackTrace();
						
					}

					@Override
					public void onSuccess(SessionInfo result) {
						SessionInfo userSessionInfo = SessionInfo.getSharedInstance();					
						userSessionInfo.setUserSessionInfo(result);
						String partyName = (String)result.getSessionData(SessionInfo.SESSION_PARTYNAME);
						String partyId = (String)result.getSessionData(SessionInfo.SESSION_PARTYID);
						String siteId = (String)result.getSessionData(SessionInfo.SESSION_SITEID);
						String siteName = (String)result.getSessionData(SessionInfo.SESSION_SITENAME);
						String groupNo = (String)result.getSessionData(SessionInfo.SESSION_SITEGROUPNO);
						String roleName = (String)result.getSessionData(SessionInfo.SESSION_ROLENAME);
						RootPanel.get("mainContainer").clear();
						if(siteId != null && siteId.trim().length() > 0)
							RootPanel.get("mainContainer").add(new LiveProductionUI(Long.valueOf(siteId),siteName,groupNo));
						
						else
							RootPanel.get("mainContainer").add(new MeterListView(Long.valueOf(partyId),roleName,"Live"));
						
						
//						RootPanel.get("mainContainer").add(new GaugeMeter(null, null,null, null));
						
						appMenuTables = new FlexTable();
						Anchor liveMenu= new CMenuItem("live", true,"Live").getMenuItem();
						FlowPanel fLive = new FlowPanel();
						fLive.getElement().setId("live");
						fLive.add(liveMenu);
						appMenuTables.setWidget(0, 0,fLive);				
						addMenuClickHandler(liveMenu, "Live");
						
						Anchor dashBoardMenu = new CMenuItem("dashboard", false,"Dash Board").getMenuItem();
						FlowPanel fDashBoard = new FlowPanel();
						fDashBoard.getElement().setId("dashboard");
						fDashBoard.add(dashBoardMenu);
						appMenuTables.setWidget(0, 1,fDashBoard);
						addMenuClickHandler(dashBoardMenu, "Dash Board");
						
						if (roleName.equalsIgnoreCase("Admin")) {
							Anchor setupMenu = new CMenuItem("setup", false,"Setup").getMenuItem();
							FlowPanel fSetup = new FlowPanel();
							fSetup.getElement().setId("setup");
							fSetup.add(setupMenu);
							appMenuTables.setWidget(0, 2, fSetup);
							addMenuClickHandler(setupMenu, "Setup");
						}
						
						Anchor reportMenu = new CMenuItem("report", false,"Report").getMenuItem();
						FlowPanel fReport = new FlowPanel();
						fReport.getElement().setId("report");
						fReport.add(reportMenu);
						appMenuTables.setWidget(0, 3,fReport);
						addMenuClickHandler(reportMenu, "Report");
						headerFlexTable.setWidget(2, 0, appMenuTables);
						headerFlexTable.getFlexCellFormatter().setStyleName(2, 0, "menu_bg");
						
//						MenuBar menuBar = new MenuBar();
//						menuBar.addStyleName("demo-MenuBar");
//						MenuItem miLive = new MenuItem("Live", false, (Command) null);
//						miLive.setCommand(new MyCommand(miLive));
//						menuBar.addItem(miLive);
//						
//						MenuItem miDashBoard = new MenuItem("Dash Board", false, (Command) null);
//						miDashBoard.setCommand(new MyCommand(miDashBoard));
//						menuBar.addItem(miDashBoard);
//						
//						if (roleName.equalsIgnoreCase("Admin")) {
//							MenuItem miSetup = new MenuItem("Setup", false, (Command) null);
//							miSetup.setCommand(new MyCommand(miSetup));
//							menuBar.addItem(miSetup);
//						}
//						
//						MenuItem miReport = new MenuItem("Report", false, (Command) null);
//						miReport.setCommand(new MyCommand(miReport));
//						menuBar.addItem(miReport);
//						menuBar.selectItem(miLive);
//						
//						VerticalPanel mainVerticalPanel = new VerticalPanel();
//						mainVerticalPanel.setWidth("900px");
//						mainVerticalPanel.add(hp);
//						mainVerticalPanel.setCellHorizontalAlignment(hp, HasHorizontalAlignment.ALIGN_LEFT);
//						RootPanel.get("mainContainer").add(mainVerticalPanel);
//						selectMenuItem("Live");
						
						loginFlexTable.setWidget(0, 0, new HTML("<span class=\"body_txt_blue2\">Hello,</span> <span class=\"body_txt_black_b\">"+partyName+"</span> <span class=\"body_txt_gray\">|</span>"));
						
						Anchor logoutAnchor = new Anchor("Logout");
						
						loginFlexTable.setWidget(0, 1, logoutAnchor);
						loginFlexTable.getFlexCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_TOP);
						loginFlexTable.getFlexCellFormatter().setStyleName(0, 1, "body_txt_black");
						logoutAnchor.addClickHandler(new ClickHandler() {
							
							@Override
							public void onClick(ClickEvent event) {
								ServiceUtil.getUserLoginServiceAsync().logout(new AsyncCallback<String>() {

									@Override
									public void onFailure(Throwable caught) {
										// TODO Auto-generated method stub
										
									}

									@Override
									public void onSuccess(String result) {
										
										loginFlexTable.setWidget(0, 0, new HTML());
										loginFlexTable.setWidget(0, 1, loginLink);
										int widgetCount = RootPanel.get("headerContainer").getWidgetCount();
										System.err.println("rowCount : " +widgetCount);
										if(widgetCount > 0){
											FlexTable flexTable = (FlexTable)RootPanel.get("headerContainer").getWidget(0);
											flexTable.getRowCount();
											flexTable.removeRow(2);
										}
										
										RootPanel.get("mainContainer").clear();
									}
								});
								
							}
						});
					}
				});
				
			}
		});
		loginFlexTable.getFlexCellFormatter().setHeight(0, 0,"25px");
		loginFlexTable.getFlexCellFormatter().setWidth(0, 0,"95%");
//		headerFlexTable.getFlexCellFormatter().setWidth(1, 1, "50px");
		loginFlexTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		loginFlexTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
		loginFlexTable.getFlexCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
		loginFlexTable.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		loginFlexTable.setCellPadding(0);
		loginFlexTable.setCellSpacing(0);
		
		
//		headerFlexTable.getFlexCellFormatter().setColSpan(0, 0,2);

//		DatePicker datePicker = new DatePicker();
//		datePicker.setStyleName("myDatePicker");
		headerFlexTable.setWidget(0, 0, loginFlexTable);
//		headerFlexTable.setWidget(0, 1,datePicker);
		headerFlexTable.setCellPadding(0);
		headerFlexTable.setCellSpacing(0);
		
		final FlexTable commonMenuTable = new FlexTable();
		commonMenuTable.setWidth("1003px");
		Anchor mnuHome = new Anchor("HOME");
		mnuHome.setStyleName("menu");
		
		Anchor mnuAbout = new Anchor("ABOUT");
		mnuAbout.setStyleName("menu");
		
		Anchor mnuSupport = new Anchor("SUPPORT");
		mnuSupport.setStyleName("menu");
		
		Anchor mnuContact = new Anchor("CONTACT");
		mnuContact.setStyleName("menu");
		commonMenuTable.setWidget(0, 0, mnuHome);
		commonMenuTable.setWidget(0, 1, mnuAbout);
		commonMenuTable.setWidget(0, 2, mnuSupport);
		commonMenuTable.setWidget(0, 3, mnuContact);
		
		commonMenuTable.getFlexCellFormatter().setWidth(0, 0, "100px");
		commonMenuTable.getFlexCellFormatter().setWidth(0, 1, "100px");
		commonMenuTable.getFlexCellFormatter().setWidth(0, 2, "100px");
		commonMenuTable.getFlexCellFormatter().setWidth(0, 3, "100px");
		
		headerFlexTable.getFlexCellFormatter().setStyleName(0, 0, "top_menu_bg");
		headerFlexTable.setWidget(1, 0, new Image("./images/REmon_logo.png"));
//		headerFlexTable.setCellPadding(7);
//		headerFlexTable.setWidget(1, 0, commonMenuTable);
		headerFlexTable.getFlexCellFormatter().setStyleName(1, 0, "logo_bg");
//		headerFlexTable.getFlexCellFormatter().setStyleName(1, 0, "logo_bg");
		headerFlexTable.getFlexCellFormatter().setColSpan(1, 0, 3);
		headerFlexTable.getFlexCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_TOP);
		headerFlexTable.getFlexCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT);
		RootPanel.get("headerContainer").add(headerFlexTable);
		
//		
//		FlexTable mainTable = new FlexTable();
////		mainTable.setWidth("950px");
//		mainTable.setBorderWidth(1);
//		VerticalPanel vp = new VerticalPanel();
//		vp.add(mainTable);
//		vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
//		RootPanel.get().add(vp);
		
		
//		RootPanel.get().set
	}
	
	
	
	public class MyCommand implements Command {

		private final MenuItem item;

		public MyCommand(MenuItem item) {
			this.item = item;
		}

		@Override
		public void execute() {
//			System.err.println(item.getText());
			selectMenuItem(item.getText());
		}
	}
	
	private void addMenuClickHandler(final Anchor html,final String menuName){
		html.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				int rCount = appMenuTables.getRowCount();
				
				for(int rIndex = 0; rIndex<rCount;rIndex++){
					int cCount = appMenuTables.getCellCount(rIndex);
					System.err.println("cCount"+cCount);
					for(int cIndex = 0; cIndex < cCount;cIndex++){
						System.err.println("cIndex"+cIndex);
						if (appMenuTables.getWidget(rIndex, cIndex) != null) {
							FlowPanel flowPanel = (FlowPanel) appMenuTables.getWidget(rIndex, cIndex);
							if (((Anchor) flowPanel.getWidget(0)).getText().equalsIgnoreCase(menuName))
								((Anchor) flowPanel.getWidget(0)).setStyleName("active");
							else
								((Anchor) flowPanel.getWidget(0)).removeStyleName("active");
						}
					}
					
				}
				
				selectMenuItem(menuName);
				
			}
		});
	}
	
	private void selectMenuItem(String menuName){
		SessionInfo userSessionInfo = SessionInfo.getSharedInstance();
		String siteId = (String)userSessionInfo.getSessionData(SessionInfo.SESSION_SITEID);
		String siteName = (String)userSessionInfo.getSessionData(SessionInfo.SESSION_SITENAME);
		String partyId = (String)userSessionInfo.getSessionData(SessionInfo.SESSION_PARTYID);
		String roleName = (String)userSessionInfo.getSessionData(SessionInfo.SESSION_ROLENAME);
		String groupNo = (String)userSessionInfo.getSessionData(SessionInfo.SESSION_SITEGROUPNO);
		if(RootPanel.get("mainContainer").getWidgetCount() > 0){
			if(RootPanel.get("mainContainer").getWidget(0) instanceof LiveProductionUI){
				((LiveProductionUI)RootPanel.get("mainContainer").getWidget(0)).cancelTimer();
			} else if(RootPanel.get("mainContainer").getWidget(0) instanceof DashBoardUI){
				((DashBoardUI)RootPanel.get("mainContainer").getWidget(0)).cancelTimer();
			}
			RootPanel.get("mainContainer").clear();
		}
		System.err.println(" menu : site id : "+siteId + " site group no : "+groupNo);
		if (siteId != null && siteId.trim().length() > 0) {
			
			if (menuName.equalsIgnoreCase("Dash Board"))
				RootPanel.get("mainContainer").add(new DashBoardUI(Long.valueOf(siteId),siteName,groupNo, "Hourly", "Utility"));
			else if (menuName.equalsIgnoreCase("Live"))
				RootPanel.get("mainContainer").add(new LiveProductionUI(Long.valueOf(siteId),siteName,groupNo));
			else if (menuName.equalsIgnoreCase("Report"))
				RootPanel.get("mainContainer").add(new ReportUI(Long.valueOf(siteId),siteName,groupNo, "Hourly", "Utility"));
		} else {
			RootPanel.get("mainContainer").add(new MeterListView(Long.valueOf(partyId), roleName, menuName));
		}			

	}
	 
}
